var dataUrl = "/postList/list";
var removeUrl = "/remove";
var createUrl = "/add";
// var updateUrl="/edit";
var updateUrl = "/edit/{id}";

var PostIDInfo = {};

// 弹出框
toastr.options = {
	closeButton : true,
	debug : false,
	progressBar : true,
	positionClass : "toast-top-center",
	onclick : null,
	showDuration : "300",
	hideDuration : "1000",
	timeOut : "2000",
	extendedTimeOut : "1000",
	showEasing : "swing",
	hideEasing : "linear",
	showMethod : "fadeIn",
	hideMethod : "fadeOut"
};

$("body").delegate(".postInfo-select a", "click", function() {
	var target = $(event.target);
	$('.areaInfoText').text(target[0].innerHTML);
	PostIDInfo.area = target[0].innerHTML;
});

$("body").delegate(".shiftInfo-select a", "click", function() {
	var target = $(event.target);
	$('.shiftInfoText').text(target[0].innerHTML);
	PostIDInfo.shift = target[0].innerHTML;
});

$("body").delegate(".edit-postInfo-select a", "click", function() {
	var target = $(event.target);
	$('.editAreaInfoText').text(target[0].innerHTML);
	PostIDInfo.area = target[0].innerHTML;
});

$("body").delegate(".edit-shiftInfo-select a", "click", function() {
	var target = $(event.target);
	$('.editShiftInfoText').text(target[0].innerHTML);
	PostIDInfo.shift = target[0].innerHTML;
});

// 清空表单数据
$('.modal').on('hidden.bs.modal', function() {
	$('#id').val('');
	$('#post').val('');
	$('.areaInfoText').text("点击选择");
	$('.shiftInfoText').text("点击选择");
	PostIDInfo = {};
});

function addPostDa() {
	PostIDInfo.id = $("#id").val();
	PostIDInfo.post = $("#post").val();
	if (!PostIDInfo.id || !PostIDInfo.post) {
		toastr['warning']('请填写完整表单！');
		return;
	}
	$.ajax({
		// cache : true,
		type : "POST",
		url : "postList/addData",
		data : PostIDInfo,
		async : false,
		error : function(request) {
			$.modal.alertError("系统错误");
		},
		success : function(data) {
			$.operate.ajaxSuccess(data);
			$('#addModal').modal('hide')

		}
	});
}

function editPostData() {
	var data = $("#dataTable").bootstrapTable("getSelections");
	if (!data || data.length == 0) {
		toastr['warning']('请选择一条数据！');
		return;
	}
	if (data.length > 1) {
		toastr['warning']('只能选择一条数据编辑！');
		return;
	}

	$("#editId").val(data[0].id);
	$("#editPost").val(data[0].post)
	$('.editAreaInfoText').text(data[0].area);
	$('.editShiftInfoText').text(data[0].shift);

	$('#editModal').modal('toggle')
}

function editPostIDDataPost() {
	PostIDInfo={};
	PostIDInfo.id = $("#editId").val();
	PostIDInfo.post = $("#editPost").val();
	PostIDInfo.area = $('.editAreaInfoText').text();
	PostIDInfo.shift = $('.editShiftInfoText').text();
	$.ajax({
		// cache : true,
		type : "POST",
		url : "postList/editData",
		data : PostIDInfo,
		async : false,
		error : function(request) {
			$.modal.alertError("系统错误");
		},
		success : function(data) {
			$.operate.ajaxSuccess(data);
			$('#editModal').modal('hide')
		}
	});
}

function deletePostData() {
	var data = $("#dataTable").bootstrapTable("getSelections");
	var ids = [];
	for (i = 0, len = data.length; i < len; i++) {
		ids.push(data[i].id);
	}
	$.ajax({
		// cache : true,
		type : "POST",
		url : "postList/remove",
		data : JSON.stringify(ids),
		async : false,
		contentType : "application/json" ,
		error : function(request) {
			$.modal.alertError("系统错误");
		},
		success : function(data) {
			$.operate.ajaxSuccess(data);
		}
	});
}

//var exportUrl = window.rootPath + "/RoleController/export";
var options = {
	dataUrl : dataUrl,
	createUrl : createUrl,
	updateUrl : updateUrl,
	removeUrl : removeUrl,
//	exportUrl : exportUrl,
	sortName : "roleSort",
	modalName : "员工信息",
	search : false,
	dataColumns : [
			{
				checkbox : true
			},
			{
				field : 'id',
				title : '岗位编号'
			},

			{
				field : 'post',
				title : '岗位名称'
			},
			{
				field : 'area',
				title : '区域'
			},
			{
				field : 'shift',
				title : '班次'
			},
			{
				title : '操作',
				formatter : function(value, row, index) {
					var id = row.id;
					var actions = [];
					actions
							.push('<a class="btn btn-success btn-xs"  href="#" onclick="editPostData()"><i class="fa fa-edit"></i>编辑</a> ');
					actions
							.push('<a class="btn btn-danger btn-xs" href="#" onclick="deletePostData()"><i class="fa fa-remove"></i>删除</a>');
					return actions.join('');

					// return '<a class="btn btn-xs btn-danger"
					// onclick="delPermission(' + "'" + id + "'" + ')"><i
					// class="fa fa-remove"></i>删除</a>';
				}
			} ]
};
$(function() {
	var oTab = $.table.oTableInit(options);
	oTab.Init();
})